package activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/*
 * 2019/1/13 15:05
 * by activemq
 */
public class Publisher {
    public static void main(String[] args) throws JMSException, InterruptedException {
        System.out.println("Publisher start");
        ActiveMQConnectionFactory factor = new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
        TopicConnection topicConnection = factor.createTopicConnection();
        topicConnection.start();
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = topicSession.createTopic("topic1");
        MessageProducer producer = topicSession.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 500; i++) {
            TextMessage text = topicSession.createTextMessage();
            text.setText("message " + i);
            producer.send(text);
            Thread.sleep(1000);
        }
        topicConnection.close();
    }
}